package com.hmall.item.web;

import cn.hutool.json.JSON;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.RequestParams;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    @GetMapping("/list")
    public PageDTO list(Integer page,Integer size){
        return itemService.getlist(page,size);
    }


    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    @PostMapping
    public void addItem(@RequestBody Item item){
        itemService.addItem(item);
    }

    /**
     * 商品的上下架
     */
    @PutMapping("/status/{id}/{status}")
    public void onOrOff(@PathVariable Long id,@PathVariable Integer status){
        itemService.onOrOff(id,status);
    }


    /**
     * 修改商品，上架状态不可修改
     * @param item
     */
    @PutMapping
    public void updateById(@RequestBody Item item){
        itemService.update1(item);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id){
        itemService.deleteById(id);
    }


    @PostMapping("/filters")
    public JSON filters(@RequestBody RequestParams requestParams){
        return itemService.filter(requestParams);
    }


    @PostMapping("/list")
    public PageDTO list(@RequestBody RequestParams requestParams){
        return itemService.list1(requestParams);
    }

    /**
     * 扣减库存
     */
    @PutMapping("/item/stock/{itemId}/{num}")
    public void changes(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.changes(itemId,num);
    }

    /**
     * 回复库存
     * @param itemId
     * @param num
     */
    @PutMapping("/item/stock1/{itemId}/{num}")
    public void changes1(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.changes1(itemId,num);
    }


}
